package com.settlement.system.service.impl;

import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblTmBillDetailMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblTmBillDetail;
import com.settlement.system.common.util.common.JsonUtil;
import com.settlement.system.service.ITblTmBillDetailService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.ResultHandler;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.sql.SQLIntegrityConstraintViolationException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 收退款-天猫账单明细 服务实现类
 *
 * @author Ximi
 * @since 2020-06-12
 */
@Service
@Slf4j
public class TblTmBillDetailServiceImpl extends BaseServiceImpl implements ITblTmBillDetailService {

    @Resource
    private TblTmBillDetailMapper tblTmBillDetailMapper;

    @Override
    public BaseMapper init() {
        return tblTmBillDetailMapper;
    }

    @Override
    public TblTmBillDetail getFoot(Map<String, Object> params) throws ServiceException {
        try {
            return tblTmBillDetailMapper.getFoot(params);
        } catch (Exception ex) {
            log.error("获取合计异常", ex);
            throw new ServiceException("获取合计异常", ex);
        }
    }

    @Override
    @Transactional(rollbackFor = {ServiceException.class},noRollbackFor = {DuplicateKeyException.class})
    public <ModelType> Integer saveNoDuplicateList(List<ModelType> list) throws ServiceException, DuplicateKeyException {
        List<TblTmBillDetail> billDetails = new ArrayList<>();
        List<TblTmBillDetail> duplicateList = new ArrayList<>();
        for (int index = 0; index < list.size(); index++) {
            billDetails.add((TblTmBillDetail) list.get(index));
            if (billDetails.size() == 50 || index == list.size() - 1) {
                try {
                    tblTmBillDetailMapper.batchAdd(billDetails);
                } catch (Exception ex) {
                    if (ex.getCause() != null && ex.getCause() instanceof SQLIntegrityConstraintViolationException) {
                        duplicateList.addAll(saveUnique(billDetails));
                    } else {
                        log.error("批量保存天猫异常", ex);
                        throw new ServiceException("批量保存天猫异常", ex);
                    }
                }
                billDetails.clear();
            }
        }
        if (duplicateList.size() > 0) {
            throw new DuplicateKeyException(JsonUtil.getJsonOrToString(duplicateList));
        }
        return list.size();
    }

    private List<TblTmBillDetail> saveUnique(List<TblTmBillDetail> data) throws ServiceException {
        List<TblTmBillDetail> duplicateList = new ArrayList<>();
        for (TblTmBillDetail info : data) {
            try {
                tblTmBillDetailMapper.insert(info);
            } catch (Exception ex) {
                if (ex.getCause() != null && ex.getCause() instanceof SQLIntegrityConstraintViolationException) {
                    duplicateList.add(info);
                } else {
                    log.error("保存天猫异常", ex);
                    throw new ServiceException("保存天猫异常", ex);
                }
            }
        }
        return duplicateList;
    }

    public void getEctDataByHandler(Map<String, Object> params, ResultHandler handler) {
        tblTmBillDetailMapper.getDataByHandler(params,handler);
    }
}

